home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr46 / strx221.zip / STR.H < prev    next >
C/C++ Source or Header  |  1993-05-18  |  11KB  |  333 lines

  1. //
  2. // str.h   : str class interface
  3. // Author  : Roy S. Woll
  4. //
  5. // Copyright (c) 1993 by Roy S. Woll
  6. // You may distribute this source freely as long as you leave all files
  7. // in their original form, including the copyright notice as is.
  8. //
  9. // Version 2.2     05/16/93  Add member function read, lowercase,
  10. //                           uppercase, and variations of pad and strip.
  11. // Version 2.01    03/01/93  Support Turbo C++ (no nested class)
  12. // Version 2.00    12/01/92
  13. //
  14. #ifndef _STR_H
  15. #define _STR_H
  16.  
  17. //#define DEBUG_STR
  18.  
  19. class ostream;
  20. class istream;
  21. class dynstream;
  22. class regX;
  23.  
  24. #ifdef __TURBOC__
  25.     #ifndef __BORLANDC__
  26.         #define _SUBSTR substr
  27.     #else
  28.         #define _SUBSTR str::substr
  29.         class str {
  30.     #endif
  31. #else
  32.     #define _SUBSTR str::substr
  33.     class str {
  34. #endif
  35.  
  36.    class substr{
  37.      friend class str;
  38.  
  39.      int posReplace;
  40.      int numReplace;
  41.      str * mystr;
  42.  
  43.      substr(const str * data, int AposReplace, int AnumReplace);
  44.  
  45.      int compare(const char *) const;
  46.      int compare(const substr&) const;
  47.      int length() const;
  48.      
  49.    public:
  50.      str & operator = (const substr& s);
  51.      str & operator = (const char * s);
  52.      
  53.      str operator+(const char *) const;
  54.      str operator+(const substr&) const;
  55.      int operator==(const char *) const;
  56.      int operator<=(const char *) const;
  57.      int operator>=(const char *) const;
  58.      int operator!=(const char *) const;
  59.      int operator< (const char *) const;
  60.      int operator> (const char *) const;
  61.      int operator==(const substr&) const;
  62.      int operator<=(const substr&) const;
  63.      int operator>=(const substr&) const;
  64.      int operator!=(const substr&) const;
  65.      int operator< (const substr&) const;
  66.      int operator> (const substr&) const;
  67.      
  68.      operator str() const;
  69.    };
  70.  
  71. #ifdef __TURBOC__
  72.     #ifndef __BORLANDC__
  73.         class str {
  74.     #endif
  75. #endif
  76.  
  77.    friend class dynstreambuf;
  78.  
  79. protected:
  80.    enum {STR_DEFAULT_MEMINCR=256};
  81.    enum {ICASE=1};
  82.  
  83.  
  84. #ifdef DEBUG_STR
  85. public:
  86.    static int dynstreamCount;
  87.    static int ObjectCount;
  88.    static int AllocationCount;
  89.    static int TotalObjectCount;
  90.    static int TotalAllocationCount;
  91. #endif
  92.  
  93.    friend class substr;
  94.  
  95.  
  96.    struct strdata{
  97.      enum {STR_DEBUG_BUFSIZE=80};
  98.      int cursize;         // cursize should be defined before buf!
  99.      int curlength;       // current length of buffer
  100.      int refcount;
  101.  
  102.      dynstream * mystream;// pointer to str's stream
  103.      char strChange;      // Internal flag indicating when the length has been
  104.                           // changed by the str class.  
  105.                           // If true, stream is notified.
  106.      char buf[STR_DEBUG_BUFSIZE];
  107.                          // buf[1] is all that is required, but we allow
  108.                          //   it to be buf[80] so that the debugger can
  109.                          //   treat it as a char *.
  110.    };
  111.  
  112.  
  113.    static int defaultFlags;
  114.  
  115.    int memsize_init;     // Initial amount of memory to allocate for buffer
  116.    int memsize_incr;     // Memory expansion increment
  117.    int flags;            // Default flags for case sensitivity, etc.
  118.  
  119.    str& _assign (const char * source, int len);
  120.    str& _concat (const char * source, int len);
  121.    int _checkMemAllocation(int requiredLen=0);
  122.  
  123.    static void init(strdata *&, int, int);
  124.  
  125. protected:
  126.  
  127.    strdata * data;
  128.    char * getNewBuffer( int len, int newbufsize);
  129.    char * getNewBuffer(int newbufsize);
  130.  
  131.    void setNewBuffer(strdata * newdata, int newbufsize, int len);
  132.  
  133.    void setlength(int len) const;   // update the current length
  134.  
  135.    int strncmp(const char * s1, const char * s2, int n) const;
  136.    int strcmp(const char * s1, const char * s2) const;
  137.  
  138. //
  139. // Special constructors used by implementation
  140. //
  141.    str(const char *, const char *);
  142.    str(const char *, const _SUBSTR&);
  143.    str(const substr&, const char *);
  144.    str(const substr&, const substr&);
  145.  
  146.  
  147. public:
  148.  
  149. //
  150. // Constructors
  151. //
  152.    str(void);
  153.    str(int p_bufsize, int = STR_DEFAULT_MEMINCR);
  154.    str(const char * s, int bufsize=0, int = STR_DEFAULT_MEMINCR);
  155.    str(const str&, int bufsize=0, int = STR_DEFAULT_MEMINCR);
  156.  
  157.    virtual ~str();
  158.  
  159. //
  160. // Access Operators
  161. //
  162.    int size(void) const;        // return current memory allocated for buffer
  163.    int length(void) const;      // return current string length of buffer
  164.  
  165.    operator const char * () const;    // for implicit casting 
  166.    const char * operator()() const;           // for explicit casting
  167.    const char * operator()(int index) const;  // 
  168.  
  169. //
  170. // String substitution using substr "() syntax"
  171. //
  172.    substr operator()(int pos, int numreplace); // create substr from str
  173.    const substr operator()(int pos, int numreplace) const;
  174.  
  175. //
  176. // String search/replace member functions
  177. //
  178.    int search (const char *, int * startPtr) const;
  179.    int search (const char *, int start=0) const;
  180.    int search (const regX&, int * startPtr) const;
  181.    int search (const regX&, int start) const;
  182.    int search (const regX&, str * matchPtr=0, int start=0) const;
  183.    int search (const regX&, str * matchPtr, int * startPtr) const;
  184.  
  185.    int index (const char *, int start=0) const;
  186.    int index (const regX&, int start=0) const;
  187.    int index (const regX&, int * matchLen, int start=0) const;
  188.  
  189.  
  190.    int replace (const regX&, const char * replaceString, 
  191.                int* startPtr, int numReplacements=1);
  192.    int replace (const char * pattern, const char * replaceString, 
  193.                int* startPtr, int numReplacements=1);
  194.    int replace (const regX&, const char * replaceString, 
  195.                int start=0, int numReplacements=1);
  196.    int replace (const char * pattern, const char * replaceString, 
  197.                int start=0, int numReplacements=1);
  198.    int replaceAll (const char *, const char * replaceString, int start=0);
  199.    int replaceAll (const regX&, const char * replaceString, int start=0);
  200.  
  201.    char& operator[](int position);            // array indexing
  202.    char operator[](int position) const;       // array indexing for const
  203.  
  204.    ostream& stream(void);    // return stream for this str
  205.    ostream& stream(int);     // return stream and move put pointer
  206.  
  207. //
  208. // Assignment & Concatenation Operators
  209. //
  210.    str & operator = (const str & s);     // s = str;
  211.    str & operator = (const substr & s);  // s = substr;
  212.    str & operator = (const char * s);    // s = charptr
  213.    str & operator = (const char s);      // s = character
  214.    str & assign (const char * source, int len=0);
  215.  
  216.    str & operator += (const str & s);    // s += str
  217.    str & operator += (const substr & s); // s += substr
  218.    str & operator += (const char * s);   // s += charptr
  219.    str & operator += (const char s);     // s += char
  220.  
  221.    str & operator << (const str& s);     // s << str
  222.    str & operator << (const substr& s);  // s << substr
  223.    str & operator << (const char * s);   // s << charptr
  224.    str & operator << (const int s);      // s << int
  225.    str & operator << (const char s);     // s << char
  226.  
  227. // Manipulators
  228.    enum PadStripT {left, right, both, leading=0, trailing};
  229.    
  230.    str& strip(PadStripT t=trailing, const char * stripchar=" \t");
  231.    str& strip(PadStripT t, char stripchar);
  232.    str& stripTrailing(const char * stripchar=" \t");
  233.    str& stripLeading(const char * stripchar=" \t");
  234.     str& stripBoth(const char * stripchar=" \t");
  235.     
  236.    str& pad(int padsize, PadStripT t=right, char padchar = ' ');
  237.    str& padRight(int padsize, char padchar = ' ');
  238.    str& padLeft(int padsize, char padchar = ' ');
  239.    str& padBoth(int padsize, char padchar = ' ');
  240.     
  241.    str& lowercase(); // convert this string to lowercase
  242.    str& uppercase(); // convert this string to uppercase
  243.  
  244.     
  245. // Mutators
  246.    int insert(int pos, char ch);       // insert ch, return 0 if fail
  247.    int insert(int pos, const char * insertStr); 
  248.                                        // insert char *, return 0 if fail
  249.    void remove(int pos, int numdel=1); // remove numdel characters starting at
  250.                                        //   position pos
  251.  
  252.    str operator+(const str &) const;
  253.    str operator+(const substr &) const;
  254.    str operator+(const char * b) const;
  255.    str operator+(const char b) const;
  256.  
  257.    int operator==(const char *) const;  // made member functions so that
  258.    int operator<=(const char *) const;  // derived classes inherit functions
  259.    int operator>=(const char *) const;
  260.    int operator!=(const char *) const;
  261.    int operator< (const char *) const;
  262.    int operator> (const char *) const;
  263.  
  264.    int operator==(const str &) const;
  265.    int operator<=(const str &) const;
  266.    int operator>=(const str &) const;
  267.    int operator!=(const str &) const;
  268.    int operator< (const str &) const;
  269.    int operator> (const str &) const;
  270.  
  271. // Friend Stream Operators
  272.     istream& read(istream&, int);
  273.     friend istream& operator >> (istream&, str &);
  274.     friend ostream& operator << (ostream&, const str &);
  275.  
  276.    friend int compare(const str&, const char *);
  277.    friend int compare(const str&, const str &);
  278.    friend int compare(const char *, const str &);
  279.  
  280.    friend str operator+(const char *, const str &); 
  281.    friend str operator+(const char *, const _SUBSTR &); 
  282.  
  283.    friend str strip(str, PadStripT t=trailing, const char * stripchar=" \t");
  284.    friend str strip(str, PadStripT t, char stripchar);
  285.    friend str pad(str s, int padsize, PadStripT t=right, char padchar= ' ');
  286.    friend str lowercase(const char *); // return lower case of string
  287.    friend str uppercase(const char *); // return upper case of string
  288.  
  289.  
  290.  //
  291.  // Case sensitivity functions
  292.  //
  293.    static void setdefaultCaseSensitive(int val);
  294.    void setCaseSensitive(int val);
  295.    int caseSensitive(void) const;
  296. };
  297.  
  298. //
  299. // Define inline functions
  300. //
  301. inline const char * str::operator()() const   // for explicit casting
  302.      {return (const char *)*this;}
  303.  
  304. inline char str::operator[](int position) const{  // array indexing
  305.    return data->buf[position];
  306. }
  307.  
  308. inline void str::setlength(int len) const{   // update the current length
  309.    data->buf[len]=0;     // null terminated
  310.    data->curlength=len;
  311.    data->strChange=1;
  312. }
  313.  
  314. //
  315. // Global member function operators
  316. //
  317.    int operator==(const char *, const str &);
  318.    int operator<=(const char *, const str &);
  319.    int operator>=(const char *, const str &);
  320.    int operator!=(const char *, const str &);
  321.    int operator< (const char *, const str &);
  322.    int operator> (const char *, const str &);
  323.  
  324.    int operator==(const char *, const _SUBSTR &);
  325.    int operator<=(const char *, const _SUBSTR &);
  326.    int operator>=(const char *, const _SUBSTR &);
  327.    int operator!=(const char *, const _SUBSTR &);
  328.    int operator< (const char *, const _SUBSTR &);
  329.    int operator> (const char *, const _SUBSTR &);
  330.  
  331.  
  332. #endif
  333.